package com.c6906.vdam.das.common;

import com.c6906.vdam.common.util.CoordTransform;
import lombok.Getter;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Component;
import tech.spiro.addrparser.io.RegionDataInput;
import tech.spiro.addrparser.io.file.JSONFileRegionDataInput;
import tech.spiro.addrparser.parser.Location;
import tech.spiro.addrparser.parser.LocationParserEngine;

@Getter
@Component
public class LocationUtil {

    private static final Logger logger = LoggerFactory.getLogger(LocationUtil.class);
    // 获取引擎实例
    private final LocationParserEngine engine;

    // 私有构造函数，防止外部实例化
    private LocationUtil(@Value("${das.regionDataPath}") String regionFilePath) {
        // china-region.json文件作为基础数据
        RegionDataInput regionDataInput = new JSONFileRegionDataInput(regionFilePath);
        // 创建并初始化位置解析引擎
        engine = new LocationParserEngine(regionDataInput);
        // 初始化，加载数据，比较耗时
        try {
            engine.init();
        } catch (Exception e) {
            logger.error("初始化地图数据失败", e);
        }
    }

    public static void main(String[] args) {
        LocationUtil locationUtil = new LocationUtil("/Users/liuwenhan/Downloads/china-region.json");
        LocationParserEngine engine = locationUtil.getEngine();
        double[] park = CoordTransform.wgs84togcj02(113.071321, 29.331713);
        Location parkLoc = engine.parse(park[0], park[1]);
        System.out.println(parkLoc.toString());
        double[] unit = CoordTransform.wgs84togcj02(113.07135728668118, 29.332427927254006);
        Location unitLoc = engine.parse(unit[0], unit[1]);
        System.out.println(unitLoc.toString());
    }
}